package com.system.borrow.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.system.borrow.MyApplication;
import com.system.borrow.R;
import com.system.borrow.activity.LoginActivity;
import com.system.borrow.activity.WebViewActivity;
import com.system.borrow.bean.GloData;
import com.system.borrow.bean.TuiJian;

import java.util.ArrayList;
import java.util.List;

/**
 * ================================================
 * 作    者：LuCenly(落尘)
 * 版    本：1.0
 * 创建日期：2016/11/30 19:38
 * 描    述：TODO
 * ================================================
 */
public class LoanListAdapter extends
        RecyclerView.Adapter<LoanListAdapter.ViewHolder>
{
    private LayoutInflater mInflater;
    private List<TuiJian> list = new ArrayList<>();
    Context context;
    public LoanListAdapter(Context context,List<TuiJian> list)
    {
        this.context = context;
        this.list = list;
        mInflater = LayoutInflater.from(context);
    }

    public void setList(List<TuiJian> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder
    {
        public ViewHolder(View arg0)
        {
            super(arg0);
        }

        ImageView iv_img_left;
        TextView tv_name_left,tv_tag_left;
        LinearLayout ll_all,ll_bg;
    }

    @Override
    public int getItemCount()
    {
        return list.size();
    }

    /**
     * 创建ViewHolder
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i)
    {
        View view = mInflater.inflate(R.layout.item_loan,
                viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.iv_img_left = (ImageView) view.findViewById(R.id.iv_img_left);
        viewHolder.tv_tag_left = (TextView) view.findViewById(R.id.tv_tag_left);
        viewHolder.tv_name_left = (TextView) view.findViewById(R.id.tv_name_left);
        viewHolder.ll_all = (LinearLayout) view.findViewById(R.id.ll_all);
        viewHolder.ll_bg = (LinearLayout) view.findViewById(R.id.ll_bg);
        return viewHolder;
    }

    /**
     * 设置值
     */
    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int i)
    {
        final TuiJian loanList = list.get(i);
        viewHolder.tv_name_left.setText(loanList.name);
        MyApplication.imageLoader.displayImage(loanList.img,viewHolder.iv_img_left);
        viewHolder.tv_tag_left.setText(loanList.tag);
        switch (loanList.special){
            case 0:
                viewHolder.ll_bg.setBackgroundResource(R.drawable.bai);
                break;
            case 1:
                viewHolder.ll_bg.setBackgroundResource(R.drawable.hot);
                break;
            case 2:
                viewHolder.ll_bg.setBackgroundResource(R.drawable.new1);
                break;
        }
        viewHolder.ll_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(GloData.getCustomerDTO()==null){
                    Intent loginIntent = new Intent(context,LoginActivity.class);
                    context.startActivity(loginIntent);
                    return;
                }
                Intent intent = new Intent(context, WebViewActivity.class);
                intent.putExtra("name",loanList.name);
                intent.putExtra("url",loanList.url);
                intent.putExtra("id",loanList.id);
                intent.putExtra("type",1);
                context.startActivity(intent);
            }
        });
    }
}
